//
//  ControlTemplateViewEnumController.m
//  Uhomebk
//
//  Created by 揭婧雯 on 17/3/3.
//  Copyright © 2017年 com.segi. All rights reserved.
//

#import "ControlTemplateViewEnumController.h"

#pragma mark -
#pragma mark 二选一控件【enum】
@interface ControlTemplateViewEnumController () {
    UIView* sexView;
}
@property (nonatomic, retain) NSString* defaultValue;

@end

@implementation ControlTemplateViewEnumController

static ControlTemplateViewEnumController * sharedManagerEnumInter = nil;

+ (ControlTemplateViewEnumController *)sharedManagerEnum
{
    sharedManagerEnumInter = [[self alloc] init];
    
    return sharedManagerEnumInter;
}

- (void)initVar
{
    _valueStr = @"";
    _param_id = [SEGUtils ValueToString:[_params valueForKey:@"attrInstId"]];
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.isNeedAdpterHotspot = YES;
    
    [self initVar];
    
    _bgMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 50)];
    _bgMainView.backgroundColor = kColor_W;
    self.tempBgView = _bgMainView;
    [self.view addSubview:_bgMainView];
    
    //标题
    UILabel* titleLabel = [[UILabel alloc] init];
    [titleLabel setText:[_params valueForKey:@"title"]];
    [titleLabel setFont:kFont_16];
    [titleLabel setTextColor:kColor_1];
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [_bgMainView addSubview:titleLabel];
    [titleLabel sizeToFit];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bgMainView.mas_left).offset(HEADSPACE);
        make.centerY.equalTo(_bgMainView.mas_centerY);
        make.width.mas_equalTo(TITLE_WIDTH);
    }];
    
    ////////判断defaultValue是否有值
    if ([[_params valueForKey:@"initData"] length] > 0) {
        _defaultValue = [_params valueForKey:@"initData"];
    }
    
    ////////是否可读 0-不显示 1-显示
    if ([[SEGUtils ValueToString:[_params valueForKey:@"readable"]] isEqualToString:@"0"]) {
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), 0);
    }
    else {
        //线条
        UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(_bgMainView.bounds), 0.5)];
        lineView.backgroundColor = kColor_L2;
        lineView.tag = 8888;
        [_bgMainView addSubview:lineView];
    }
    
    ////////是否必填 //是否可读 1-必须 0-可不填
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    
    NSArray * initArray = [SEGUtils arrayWithJsonString:[_params valueForKey:@"initData"]];
    NSString * attrValueStr = [_params valueForKey:@"attrValue"];
    _valueStr = attrValueStr;
    NSDictionary * defaultValueDic = [[NSDictionary alloc] init];
    NSString * defaultValueString = @"";
    int i =0 ;
    for (NSDictionary * dic in initArray) {
        NSString * _idStr = dic[@"id"];
        if (attrValueStr.length >0) {
            if ([_idStr isEqualToString:attrValueStr]) {
                defaultValueDic = dic;
                defaultValueString = dic[@"value"];
            }
        }else{
            if ([dic[@"isDefault"] intValue] == 1) {
                defaultValueDic = dic;
                defaultValueString = dic[@"value"];
            }
        }
        i++;
    }
    
    ////////是否必填 //是否可读 0-可不写 1-可写
    if ([[SEGUtils ValueToString:[_params valueForKey:@"writable"]] isEqualToString:@"0"]) {
        
        NSDictionary * dataDic;
        if (defaultValueDic.allKeys.count >0) {
            dataDic = defaultValueDic;
        }
        
        UILabel* contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(TITLE_WIDTH + HEADSPACE, 0, CGRectGetWidth(_bgMainView.bounds) - 102, 50)];
        [contentLabel setText:initArray.count>0?[dataDic valueForKey:@"value"]:@""];
        [contentLabel setFont:kFont_16];
        [contentLabel setTextColor:kColor_1];
        [_bgMainView addSubview:contentLabel];
        
    }
    else {
        CGFloat width = (CGRectGetWidth(_bgMainView.bounds) - TITLE_WIDTH) / [initArray count];
        
        sexView = [[UIView alloc] initWithFrame:CGRectMake(TITLE_WIDTH + HEADSPACE, 0, CGRectGetWidth(_bgMainView.bounds) - TITLE_WIDTH, 50)];
        [_bgMainView addSubview:sexView];
        
        UIImage* imageDefualt = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_radio_pre")];
        for (int i = 0; i < [initArray count]; i++) {
            NSDictionary * oneDic = [initArray objectAtIndex:i];
            NSString* sexName = oneDic[@"value"];
            
            UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_radio_nor")];
            
            UIImageView* iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * width, (CGRectGetHeight(sexView.bounds) - image.size.height) / 2, image.size.width, image.size.height)];
            iconImageView.tag = 100 + i;
            [iconImageView setImage:image];
            [sexView addSubview:iconImageView];
            
            UILabel* title1 = [[UILabel alloc] initWithFrame:CGRectMake(HEADSPACE, 0, 100, CGRectGetHeight(iconImageView.bounds))];
            [title1 setText:sexName];
            [title1 setTextColor:kColor_1];
            [title1 setFont:kFont_16];
            [iconImageView addSubview:title1];
            
            CustomButtonView* abtn1 = [[CustomButtonView alloc] initWithFrame:CGRectMake(i * width, 0, width, 50)];
            abtn1.tag = 100 + i;
            [abtn1 addTarget:self action:@selector(onBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            abtn1.tempData = oneDic[@"id"];
            [sexView addSubview:abtn1];
            
            if ([oneDic[@"id"] isEqualToString:attrValueStr]) {
                [iconImageView setImage:imageDefualt];
                _valueStr = oneDic[@"id"];
                _optional = @"0";
            }
            
            
        }
        
        
        
    }
}

- (void)onBtnAction:(CustomButtonView*)sender
{
    UIView* aView = [sender.superview.superview.subviews objectAtIndex:2];
    
    for (UIImageView* aimageView in aView.subviews) {
        if ([aimageView isKindOfClass:[UIImageView class]]) {
            if (sender.tag == aimageView.tag) {
                [aimageView setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_radio_pre")]];
            }
            else {
                [aimageView setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_radio_nor")]];
            }
        }
    }
    
    NSString* valueString = sender.tempData;
    
    _valueStr = valueString;
    _optional = @"0";
}

/**
 *  给二选一控件填充值
 *
 *  @param valueStr 接收字符串值
 */
- (void)paddingEnumView:(NSString*)valueStr
{
    _valueStr = valueStr;
    
    if ([_valueStr length] > 0) {
        _optional = @"0";
    }
    else {
        _optional = @"1";
    }
    
    NSArray* sexArray = [SEGUtils arrayWithJsonString:_params[@"initData"]];
    
    for (int i = 0; i < [sexArray count]; i++) {
        NSDictionary* sexNameDic = [sexArray objectAtIndex:i];
        
        if ([sexNameDic[@"id"] isEqualToString:valueStr]) {
            for (UIImageView* aImageView in sexView.subviews) {
                if ([aImageView isKindOfClass:[UIImageView class]]) {
                    if (aImageView.tag == 100 + i) {
                        [aImageView setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_radio_pre")]];
                        _valueStr = valueStr;
                        _optional = @"0";
                    }
                    else {
                        [aImageView setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_radio_nor")]];
                    }
                }
            }
        }
    }
}

@end

